package cn.dnui_zjq823.dnuifood_16110100823.controller;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.ResultBean;
import cn.dnui_zjq823.dnuifood_16110100823.bean.UserBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.UserModel;

public class RegisterActivity extends BaseActivity implements View.OnClickListener,onRetrofitListener<ResultBean> {
    private EditText register_username,register_password,register_mobile,register_addr,register_comment;
    private Button register_ok,register_res;
    private UserModel model;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    void initViews() {
        layout_file=R.layout.activity_register;
        setLayout(layout_file);
        register_username=findViewById(R.id.register_username);
        register_password=findViewById(R.id.register_password);
        register_mobile=findViewById(R.id.register_mobile);
        register_addr=findViewById(R.id.register_addr);
        register_comment=findViewById(R.id.register_commemt);
        register_ok=findViewById(R.id.register_ok);
        register_res=findViewById(R.id.register_res);
    }

    @Override
    void initEvents() {
        register_ok.setOnClickListener(this);
        register_res.setOnClickListener(this);
    }

    @Override
    void initDatas() {

    }

    @Override
    public void onClick(View view) {
        String username="",password="",mobile="",address="",comment="";
        switch (view.getId()){
            case R.id.register_ok:
                username=register_username.getText().toString();
                password=register_password.getText().toString();
                mobile=register_mobile.getText().toString();
                address=register_addr.getText().toString();
                comment=register_comment.getText().toString();
                model=new UserModel();
                model.register(username,password,mobile,address,comment,this);
                break;
            case R.id.register_res:
                register_username.setText("");
                register_password.setText("");
                register_mobile.setText("");
                register_addr.setText("");
                register_comment.setText("");
                break;
        }
    }

    @Override
    public void onSuccess(ResultBean object, int flag) {
        String result=object.getSuccess();
        System.out.println("---"+result);
        if(result.equals("1")){
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            finish();
        }
        else
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFalure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
